package view
{
	import com.bit101.components.VBox;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	
	import models.ModelLocator;
	
	public class BorderList extends VBox
	{
		//private var ary:Array = ModelLocator.getInstance().boderAry;
		private var model:ModelLocator = ModelLocator.getInstance();
		private var txt:TextField;
		public function BorderList(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0)
		{
			super(parent, xpos, ypos);
		}
		private function inits():void{
			this.y = 0;
			CanvasFrame.getInstance().clear();
			if(txt && txt.parent){
				this.removeChild(txt);
				txt = null;
			}
			while(this.numChildren){
				this.removeChildAt(0);
			}
			var ary:Array = ModelLocator.getInstance().boderAry;
			var btn:TextButton = new TextButton(this);
			btn.visible = false;
			for each (var prop:Object in ary)
			{
				btn = new TextButton(this,"<p align='center'><a href='event:myevent'><font>" + prop.name + "</font></a></p>");
				btn.addEventListener(MouseEvent.CLICK,onClick);
				btn.data = prop;
				btn.x = 13;
			}
			btn = new TextButton(this);
			btn.visible = false;
			this.dispatchEvent(new Event("resizeevent"))
		}
		public function setData():void{
			if(model.boderAry == null){
				//loadType(model.size);
				if(model.isLoaderboderAry){
					initTxt("加载中……");
				}else{
					initTxt("你还未选择尺寸！");
				}
				
			}else{
				inits();
			}
		}
		private function onClick(event:MouseEvent):void{
			CanvasFrame.getInstance().setData(event.target.data.list);
		}
		private function loadType(str:String):void{
			model.boderAry = null;
			var urlloader:URLLoader = new URLLoader();
			urlloader.addEventListener(Event.COMPLETE,onComplete)
			str = model.config.type //+ "?size=" + str;
			urlloader.load(new URLRequest(str));
		}
		private function onComplete(event:Event):void{
			var xml:XML = XML(event.target.data);
			model.setBoderAry(xml);
			inits();
		}
		private function initTxt(str:String):void{
			txt = new TextField();
			txt.width = 150;
			txt.height = 25;
			txt.textColor = 0xaa503c;
			this.addChild(txt);
			txt.selectable = false;
			txt.text = str;
		}
		
	}
}